package com.bkgy.modular.cwTargetSpeed.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.cwTargetSpeed.entity.CWTargetSpeed;
import com.bkgy.modular.cwTargetSpeed.mapper.CWTargetSpeedMapper;
import com.bkgy.modular.cwTargetSpeed.service.CWTargetSpeedService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @ClassName CWTargetSpeedServiceImpl
 * @date 2023/6/1 10:23
 */
@Service
public class CWTargetSpeedServiceImpl extends ServiceImpl<CWTargetSpeedMapper, CWTargetSpeed> implements CWTargetSpeedService {

    @Autowired
    private CWTargetSpeedMapper cwTargetSpeedMapper;

    @Override
    public ResultData findByData(JSONObject jsonObject) {
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");
        JSONObject condition = jsonObject.getJSONObject("condition");

        if (null == pageIndex || null == pageSize) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", -1);
        }
        List<CWTargetSpeed> byData = cwTargetSpeedMapper.findByData((pageIndex - 1) * pageSize, pageSize * pageIndex, condition);
        Integer count = cwTargetSpeedMapper.findByDataCount(condition);

        return ResultData.ResultDataSuccess(byData,count);
    }

    @Override
    public ResultData updateByData(JSONObject jsonObject) {
        String entid = jsonObject.getString("entid");
        if (!StringUtils.hasText(entid)) {
            return ResultData.ResultDataFaultSelf("参数有误",-1);
        }
        JSONObject condition = jsonObject.getJSONObject("condition");
        cwTargetSpeedMapper.updateByData(entid, condition);
        return ResultData.ResultDataFaultSelf("操作成功",1);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 创建表格数据集合
        List<CWTargetSpeed> listExcel = new ArrayList<>();// 调用service查询出数据
        List<CWTargetSpeed> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = cwTargetSpeedMapper.findByData((i - 1) * pageSize, i * pageSize, condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (CWTargetSpeed item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        ExcelUtils.downloadExcel(fileNames,response,CWTargetSpeed.class,listExcel);
    }
}
